package com.netease.xone.widget.listview;

import java.util.ArrayList;
import java.util.List;

import android.widget.BaseExpandableListAdapter;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshExpandableListView;
import com.handmark.pulltorefresh.library.X1LoadingAdapterViewBase;

public class MultiExpandableAdapterManager {

//	private ListView mListView;
	private PullToRefreshExpandableListView mRrefreshList;
	private List<BaseExpandableListAdapter> mAdapterList;
	private List<ListViewStatus> mStatusList;

	private int mCurrentAdapterIndex = -1;//当前使用adapter的索引
	private int mCommonHeaderCount = 1;//通用header的数量

	public MultiExpandableAdapterManager(PullToRefreshExpandableListView refreshList) {
		mRrefreshList = refreshList;
		mAdapterList = new ArrayList<BaseExpandableListAdapter>();
		mStatusList = new ArrayList<MultiExpandableAdapterManager.ListViewStatus>();
	}

	public void setCommonHeaderCount(int count){
		mCommonHeaderCount = count;
	}

	public void addAdapter(BaseExpandableListAdapter adapter){
//		if(mAdapterList.size() == 0){
//			mCurrentAdapterIndex = 0;
//		}
		mAdapterList.add(adapter);
		mStatusList.add(new ListViewStatus());
	}

	public void setCurrentAdapter(int index){
		if(index < 0 || index > getCount() || mAdapterList.get(index) == null){
			//Error
			return;
		}
		//Save before
		if(mCurrentAdapterIndex >= 0 && mStatusList.get(mCurrentAdapterIndex) != null){
			//Save position
			if(mRrefreshList.getRefreshableView().getFirstVisiblePosition() <= mCommonHeaderCount){

			}else{
				mStatusList.get(mCurrentAdapterIndex).savePosition(mRrefreshList.getRefreshableView());
			}
			//Save status
			mStatusList.get(mCurrentAdapterIndex).saveStatus(mRrefreshList);
		}
		//Load
		mCurrentAdapterIndex = index;

		if(mRrefreshList.getRefreshableView().getFirstVisiblePosition() <= mCommonHeaderCount){
			//load common status
			ListViewStatus status = new ListViewStatus();
			status.savePosition(mRrefreshList.getRefreshableView());
			mRrefreshList.getRefreshableView().setAdapter(mAdapterList.get(mCurrentAdapterIndex));
			status.loadPosition(mRrefreshList.getRefreshableView());
		}else{
			//load save status
			mRrefreshList.getRefreshableView().setAdapter(mAdapterList.get(mCurrentAdapterIndex));
			ListViewStatus status = mStatusList.get(mCurrentAdapterIndex);
			if(status.itemPosition <= mCommonHeaderCount){
				status.itemPosition = mCommonHeaderCount + 1;
				status.topPosition = 0;
			}
			status.loadPosition(mRrefreshList.getRefreshableView());
		}

		mStatusList.get(mCurrentAdapterIndex).loadStatus(mRrefreshList);
	}

	public int getCurrentAdapterIndex(){
		return mCurrentAdapterIndex;
	}

	public int getCount(){
		return mAdapterList.size();
	}

	private class ListViewStatus {
		// 当前ListView加载状态
		boolean hasMore;
		int loadingStatus = X1LoadingAdapterViewBase.STATE_IDLE;
		// 当前位置状态
		int itemPosition;//当前item位置
		int topPosition;//当前第一个item距离top的高度

		public void savePosition(ListView list){
			if(list == null)return;
			itemPosition = list.getFirstVisiblePosition();
			topPosition = list.getChildAt(0) == null?0:list.getChildAt(0).getTop();
		}

		public void loadPosition(ListView list){
			if(list == null)return;
			list.setSelectionFromTop(itemPosition, topPosition);
		}

		public void saveStatus(PullToRefreshExpandableListView refreshList){
			loadingStatus = refreshList.getLoadingState();
			hasMore = refreshList.hasMore();
		}

		public void loadStatus(PullToRefreshExpandableListView refreshList){
			refreshList.setHasMore(hasMore);
			refreshList.setLoadingState(loadingStatus);
		}
	}

}
